<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px" @submit.prevent>
      <el-form-item label="盘点任务单号" prop="inventoryCheckCode">
        <el-input
          v-model.trim="queryParams.inventoryCheckCode"
          placeholder="请输入盘点任务单号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>

      <el-form-item label="任务类型" prop="qualityType">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择任务类型"
          clearable
          style="width: 175px"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in check_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="厂区" prop="inventoryCheckCode">
        <el-input
          v-model.trim="queryParams.inventoryCheckCode"
          placeholder="请输入厂区"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>      
      <el-form-item label="任务状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择任务状态"
          clearable
          style="width: 175px"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            
          />
        </el-select>
      </el-form-item>
            
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:header:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:header:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:header:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:header:export']"
        >导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="downloadTemplate"
        >下载导入模板</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <MyUpload
          buttonName="导入模板"
          url="/api/inventory/check/header/import"
          @success="handleUploadSuccess"
          @warning="handleUploadWarning"
          />
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <TableScrollContainer :topHeight="220">
      <template #default="{ tableHeight }">
        <el-table v-loading="loading" :data="headerList" @selection-change="handleSelectionChange" :height="tableHeight" border>
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="盘点任务单号" align="center" prop="inventoryCheckCode" />
          <el-table-column label="任务类型" align="center" prop="type">
            <template #default="scope">
              <dict-tag :options="check_type" :value="scope.row.type"></dict-tag>
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" align="center" prop="note" /> -->
          <el-table-column label="厂区" align="center" prop="plantCode" />
          <el-table-column label="仓别" align="center" prop="warehouseType" />        
          <el-table-column label="任务状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="check_status" :value="scope.row.status"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column label="开始时间" align="center" prop="startTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.startTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="完成时间" align="center" prop="completeTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.completeTime) }}</span>
            </template>
          </el-table-column>      
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
            <template #default="scope">
              <el-button type="primary" @click="checkDetail(scope.row)">查看详情</el-button>
              <!-- <el-button type="success" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:header:edit']">修改</el-button> -->
              <el-button type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:header:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page-sizes="[10, 30, 50, 100]"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
    </TableScrollContainer>
    

    <!-- 添加或修改盘点单头对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" :close-on-click-modal="false">
      <el-form ref="headerRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间"
            style="width: 100%"
            >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Header">
import { listHeader, getHeader, delHeader, addHeader, updateHeader } from "@/api/work/checkOrder"
import TableScrollContainer from '@/components/Pagetable/TableScrollContainer.vue'

const { proxy } = getCurrentInstance()
const { check_status, check_type } = proxy.useDict('check_status', 'check_type')

const headerList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inventoryCheckCode: null,
    type: null,
    status: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

const checkDetail = (row) => {
  proxy.$router.push(`/instore/checkOrderDetail?inventoryCheckCode=${row.inventoryCheckCode}`)
}

/** 查询盘点单头列表 */
function getList() {
  loading.value = true
  listHeader(queryParams.value).then(res => {
    if (res.errCode === '1') {
      headerList.value = res.data.page.records
      total.value = res.data.page.total
      loading.value = false
    }
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    inventoryCheckCode: null,
    type: null,
    note: null,
    status: null,
    createdBy: null,
    updateBy: null,
    startTime: null,
    updateTime: null,
    createTime: null
  }
  proxy.resetForm("headerRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.inventoryCheckCode)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加盘点单头"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _inventoryCheckCode = row.inventoryCheckCode || ids.value
  getHeader(_inventoryCheckCode).then(res => {
    form.value = res.data.header
    open.value = true
    title.value = "修改盘点单头"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["headerRef"].validate(valid => {
    if (valid) {
      if (form.value.inventoryCheckCode != null) {
        updateHeader(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      } else {
        addHeader(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _inventoryCheckCodes = row.inventoryCheckCode || ids.value
  proxy.$modal.confirm('是否确认删除盘点单头编号为"' + _inventoryCheckCodes + '"的数据项？').then(function() {
    return delHeader(_inventoryCheckCodes)
  }).then(res => {
    if (res.errCode === '1') {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    } else {
      proxy.$modal.msgError(res.msg)
    }
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('api/inventory/check/header/export', {
    ...queryParams.value
  }, `header数据_${new Date().getTime()}.xlsx`)
}

getList()

// 下载模板
const downloadTemplate = async() => {
  proxy.download('/api/inventory/check/header/downloadTemplate', {}, `header模板_${new Date().getTime()}.xlsx`)
}
// 上传成功后的回调
const handleUploadSuccess = (res) => {
  getList()
}
// 部分成功回调
const handleUploadWarning = (res) => {
  getList()
}
</script>
